import { isFunctionDeclaration, isIdentifier, Node, isObjectMethod, isExportDefaultDeclaration } from '@babel/types';

/**
 * 获取函数名
 */
export function getFunctionName(f: Node) {
    let name;
    if (isFunctionDeclaration(f)) {
        if (isIdentifier(f.id)) {
            name = f.id.name;
        }
    } else if (isObjectMethod(f)) {
        if (isIdentifier(f.key)) {
            name = f.key.name;
        }
    } else if (isExportDefaultDeclaration(f)) {
        if (isFunctionDeclaration(f.declaration)) {
            if (f.declaration.id) {
                name = f.declaration.id.name;
            } else {
                name = '';
            }
        }
    }
    return name || 'default';
}
